﻿using _1._12._2015_College.Data;
using _1._12._2015_College.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data.Entity.Migrations;
using _1._12._2015_College.Data.Models;

namespace _1._12._2015_College.Controllers
{
    public class CollegeController : ApiController
    {
        public IHttpActionResult Get()
        {
            List<CollegeViewModel> models = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                models = db.Colleges.Select(c => new CollegeViewModel
                    {
                        Name = c.Name,
                        State = c.State,
                        City = c.City,
                        AvgRating = c.AvgRating,
                        CollegeId = c.CollegeId
                    }).ToList();
            }
            return Ok(models);
        }

        public IHttpActionResult Get(int id)
        {
            CollegeViewModel collegeModel = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                College college = db.Colleges.FirstOrDefault(c => c.CollegeId == id);
                collegeModel.Name = college.Name;
                collegeModel.State = college.State;
                collegeModel.City = college.City;
                collegeModel.AvgRating = college.AvgRating;
                collegeModel.CollegeId = college.CollegeId;

            }
            return Ok(collegeModel);
        }

        public IHttpActionResult Post(CollegeViewModel model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Colleges.AddOrUpdate(c => c.Name,
                    new College
                {
                    Name = model.Name,
                    State = model.State,
                    City = model.City,
                    AvgRating = model.AvgRating
                });
                db.SaveChanges();
            }
            return Ok();
        }

        public IHttpActionResult Put(CollegeViewModel model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                College college = db.Colleges.FirstOrDefault(c => c.CollegeId == model.CollegeId);
                college.Name = model.Name;
                college.State = model.State;
                college.City = model.City;
                college.State = model.State;
                college.AvgRating = model.AvgRating;

                db.SaveChanges();
            }
            return Ok();
        }

        public IHttpActionResult Delete(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                College college = db.Colleges.FirstOrDefault(c => c.CollegeId == id);
                db.Colleges.Remove(college);
                db.SaveChanges();
            }
            return Ok();
        }



    }
}
